<?php

namespace App\Store\Controllers\Basic;

use App\Store\Models\Order\OrderDeposit;
use App\Store\Models\Order\OrderMaintenance;
use App\Store\Models\Order\OrderRecharge;
use App\Store\Models\Order\OrderRecovery;
use App\Store\Models\Order\OrderSales;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Basic\PaytypeList;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Validate\Basic\PaytypeList as ValidateBasicPaytypeList;

/**
 * 支付方式信息
 */
class PaytypeListController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = PaytypeList::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        if ($id < 0) {
            $info = [
                'id' => -1,
                'paytype_name' => '混合支付',
                'bank_name' => '',
                'bank_card_no' => '',
                'bank_card_holder' => ''
            ];
        } else {
            $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
            $info = PaytypeList::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
            if (!$info) {
                return response()->json(['code' => '001', 'msg' => '参数错误']);
            }
        }

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['paytype_name', 'bank_name', 'bank_card_no', 'bank_card_holder', 'remark']);

        $Validate = new ValidateBasicPaytypeList();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $isexist = PaytypeList::where([['paytype_name', $req_data['paytype_name']], ['cp_id', $accountinfo['cp_id']]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '支付方式不允许重复']);
        }

        if ($req_data['paytype_name'] == '混合支付') {
            return response()->json(['code' => '001', 'msg' => '支付方式不允许重复']);
        }
        $data = $req_data;
        $data['create_account_id'] = $accountinfo['id'];
        $data['update_account_id'] = $accountinfo['id'];
        $data['create_account_real_name'] = $accountinfo['real_name'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        $data['cp_id'] = $accountinfo['cp_id'];
        $data['create_at'] = time();
        $data['update_at'] = time();
        PaytypeList::create($data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'paytype_name', 'bank_name', 'bank_card_no', 'bank_card_holder', 'remark']);

        $Validate = new ValidateBasicPaytypeList('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $info = PaytypeList::where([['id', $req_data['id']], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $isexist = PaytypeList::where([['id', '<>', $req_data['id']], ['paytype_name', $req_data['paytype_name']], ['cp_id', $accountinfo['cp_id']]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '支付方式不允许重复']);
        }

        if ($req_data['paytype_name'] == '混合支付') {
            return response()->json(['code' => '001', 'msg' => '支付方式不允许重复']);
        }

        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();
        $data['update_account_id'] = $accountinfo['id'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        PaytypeList::where(['id' => $req_data['id']])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }

    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = PaytypeList::where([['id', $id], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        $where['pay_type_id'] = $id;
        $where['cp_id'] = $cp_id;
        $word = 'paytype_' . $id;

        $deposit = OrderDeposit::query()
            ->where($where)
            ->orWhere('mixed_payment', 'like', '%"' . $word . '"%')
            ->first();

        if (!empty($deposit)) {
            return response()->json(['code' => '000', 'msg' => '该支付方式在开单定金表中使用无法删除']);
        }

        $sales = OrderSales::query()
            ->where($where)
            ->orWhere('mixed_payment', 'like', '%"' . $word . '"%')
            ->first();

        if (!empty($sales)) {
            return response()->json(['code' => '000', 'msg' => '该支付方式在开单销售表中使用无法删除']);
        }

        $recovery = OrderRecovery::query()
           ->where($where)
           ->orWhere('mixed_payment', 'like', '%"' . $word . '"%')
           ->first();

        if (!empty($recovery)) {
            return response()->json(['code' => '000', 'msg' => '该支付方式在开单回收表中使用无法删除']);
        }
        $maintenance = OrderMaintenance::query()
            ->where($where)
            ->orWhere('mixed_payment', 'like', '%"' . $word . '"%')
            ->first();

        if (!empty($maintenance)) {
            return response()->json(['code' => '000', 'msg' => '该支付方式在开单维修表中使用无法删除']);
        }

        $recharge = OrderRecharge::query()
            ->where($where)
            ->orWhere('mixed_payment', 'like', '%"' . $word . '"%')
            ->first();

        if (!empty($recharge)) {
            return response()->json(['code' => '000', 'msg' => '该支付方式在开单充值表中使用无法删除']);
        }



        PaytypeList::where([['id', $id]])->update(['is_del' => 2, 'del_at' => time()]);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
